var app =getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		swiperList: [],
		page:1,
		newslist:[],
		twoNews:[],
		threeNews:[],
		searchvalue:'',
		isDuaration:true,
		TabCur:0,
		scrollLeft:0,
		category: [
			{
				name: '推荐',
				id: 0,
				data: [],
				page: 1
			}
		],
	},

	searchlist(e) {
		this.setData({
			searchvalue: e.detail.value
		})
	},
	searchdesc(){
		var that = this;
		var value = that.data.searchvalue;
		wx.showLoading({
			title: '搜索中',
		})
		if(value == ''){
			wx.showToast({
				title: '请输入搜索内容',
				duration:2000,
				icon:'none'
			})
		}else{
			var setid = app.globalData.cityCode;
			app.core('get', 'search_zx_by_tl/site_id/' + setid +'/limit/5',{
				keywords: value
			},(res=>{
				// console.log(res)
				if(res.data.length == 0){
					wx.showToast({
						title: '暂无相关资讯',
						duration:2000,
						icon:'none'
					})
				}else{
					wx.navigateTo({
						url: './zixunsearch?keyword='+value+'',
						success(){
							that.setData({
								searchvalue:''
							})
						}
					});
				}
				wx.hideLoading()
			}))
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let _this = this;
		// _this.guanggao();
		// _this.list(1)

		this.category()
		setTimeout(() => {
			_this.setData({
				isDuaration:false
			})
		},1000)

		// console.log(app.globalData.cityCode);
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},
	category() {
		let that = this
		app.core('get', 'article/category' ,"",(res=>{
				that.setData({
					category: res.data,
				})
		}))
	},
	guanggao() {
		var that = this;
		var setid = app.globalData.cityCode;
		app.core('get', 'get_banner_advert/site_id/' + setid +'', '', (res) => {
			// console.log(res)
			that.setData({
				swiperList: res.data
			});
		})
	},
	list(num){
		// wx.showLoading({
		//   title: '正在加载',
		// })
		var that = this;
		var num = that.data.page;
		var list =that.data.newslist;
		var setid = app.globalData.cityCode;
		app.core('get', 'get_hot_news/site_id/' + setid +'/page/' + num ,"",(res=>{
			// console.log(res)
			that.setData({
				twoNews: res.ztc,
				threeNews:res.dtp,
			})
			var arrs = res.data;
			for(var i =0; i<arrs.length;i++){
				var time = that.getLocalTime(arrs[i].createTime )
				arrs[i].createTime = time;
				list.push(arrs[i])
			}

			var x = list;
			var y =[];
			for(var i=0;i<x.length;i++){
				if(i<2){
					y.push(x[i])
				}
			}
			that.setData({
				newslist:y,
				page: num+1,
			})
			// setTimeout(() => { wx.hideLoading()},500)
		}))
	},
	listmore(page){
		wx.showLoading({
			title: '正在加载',
		})
		var that = this;
		// var nummore = 5;
		var num = that.data.page;
		var setid = app.globalData.cityCode;
		app.core('get', 'get_hot_news/site_id/' + setid + '/page/' + num ,"", (res => {
			console.log(res.data)
			that.setData({
				// newslist: res.data
				// 后台接口请求的数据不是在原有基础上继续请求而是直接凭借
				newslist: that.data.newslist.concat(res.data)
			})
			that.setData({
				page: num + 1,
			})
		}))
		setTimeout(() => { wx.hideLoading() }, 500)
	},


	//获取资讯列表
	getLocalTime(date) {
		// return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
		var date = new Date(date * 1000);//如果date为13位不需要乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
		return Y + M + D + h + m + s;


	}, // 转换时间戳
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		var key = wx.getStorageSync('citykey');
		if (key == true) {
			this.guanggao();
			this.list(1)
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		if (this.data.TabCur == 0) {
			var that = this;
			var num = that.data.page;
			console.log(num);
			that.listmore(num)
		} else {
			this.article()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	advJump (e) {

		let swiperList = this.data.swiperList

		let val = swiperList[e.currentTarget.dataset.index]
		if (val) {
			if ( val['app_id'] != undefined && val['app_id'] != null && val['app_id'] != '') {
                wx.navigateToMiniProgram({
                    appId: val['app_id'],
                    path: val['redirect_url'],
                    extraData: {},
                    envVersion: 'release',
                    success(res) {
                        console.log(res)
                        // 打开成功
                    },
                    fail(e) {
                        console.log(e)
                    }
                })
			} else if ( val['redirect_url'] != undefined && val['redirect_url'] != null && val['redirect_url'] != '') {

                let url = val['redirect_url']

                if (url.indexOf("/pages") == 0) {
                    wx.navigateTo({
                        url: url,
                    })
                } else if (url.indexOf("http") == 0) {
                    wx.navigateTo({
                        url: '/pages/webView/index?url='+ decodeURIComponent(url),
                    })
                }
			}

		}

	},

	tabSelect(e) {
		this.setData({
			TabCur: e.currentTarget.dataset.id,
			scrollLeft: (e.currentTarget.dataset.id-1)*60
		})

		if (this.data.category[this.data.TabCur]['page'] == 1) {
			this.article()
		}

	},

	article(){
		var that = this;

		var category = that.data.category;
		var page = category[that.data.TabCur]['page'];
		var setid = app.globalData.cityCode;
		app.core('get', 'article/index?siteId=' + setid +'&page=' + page + '&categoryId=' + category[that.data.TabCur]['id'] ,"",(res=>{

			category[that.data.TabCur]['page'] ++
			category[that.data.TabCur]['data'] = [...category[that.data.TabCur]['data'], ...res.data.data]

			that.setData({
				category: category,
			})
		}))
	},
})
